package com.aimei.www.modules.exception;

import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
/*该中处理方法得到的全部是200异常*/
public class AimeiExceptiponHander {
       /*用来处理自定义异常*/
       @ExceptionHandler(AimeiException.class)
       @ResponseBody
       /**
       * @author xiah
       * description:拦截所有自定义异常
       * date: 2018/5/4
       */
       public Map getExceptionMessage(HttpServletRequest httpServletRequest, HttpServletResponse response,
                                      ErrorAttributes
                                              errorAttributes,
                                      AimeiException e) {
              Map map = new HashMap();
              //response.setStatus(Integer.parseInt(e.code));
              map.put("success", "false");
              map.put("code", e.code);
              map.put("message", e.getMessage());
              return map;
       }

       @ExceptionHandler(Exception.class)
       @ResponseBody
       /**
        * @author xiah
        * description:拦截所有自定义异常
        * date: 2018/5/4
        */
       public Map getExceptionMessage(HttpServletRequest httpServletRequest, HttpServletResponse response, Exception
               e) {
              Map map = new HashMap();
              //response.setStatus(500);
              map.put("success", "false");
              map.put("code", 500);
              map.put("message", e.getMessage());
              return map;
       }
}
